<?php

/**
 * Created by PhpStorm.
 * User: A510L
 * Date: 2018/1/23
 * Time: 8:37
 */
class Menu extends CI_Controller
{

    public function index()
    {
        $this->load->view('menu/allMenus');
    }

    public function addMenu()
    {
        $this->load->view('menu/addMenu');
    }

    public function updateMenu()
    {
        $this->load->view('menu/updateMenu');
    }

    public function select()
    {
        $this->load->view('menu/selectPic');
    }

    public function delete()
    {
        $this->load->view('menu/deleteMenu');
    }

    public $token_data;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('menu_model');
        $this->token_data = check_token($this);
    }

    /**
     * 查询菜单权限接口
     */
    public function imp_index()
    {
        if ($this->token_data['tag']) {
            $data = $this->menu_model->get_All();
            $this->token_data['data'] = $data;
        }
        $json = json_encode($this->token_data['data']);
        echo $json;
    }

    /**
     * 添加接口
     */
    public function imp_add()
    {
        if ($this->token_data['tag']) {
            $data = $_POST['data'];
            $arr = json_decode($data, true);
            $this->menu_model->addRole($arr);
            $this->token_data['success'] = 1;
        }
        $json = json_encode($this->token_data);
        echo $json;
    }

    /**
     * 回显接口
     */
    public function imp_edit(){
        if ($this->token_data['tag']) {
            $menu_id = $_POST['menu_id'];
            $data = $this->menu_model->getMenuById($menu_id);
            $this->token_data['data'] = $data;
        }
        $json = json_encode($this->token_data['data']);
        echo $json;
    }

    /**
     * 修改接口
     */
    public function imp_update(){
        if($this->token_data['tag']) {
            $data = $_POST['data'];
            $arr = json_decode($data, true);
            $this->menu_model->update_menu($arr);
            $this->token_data['success'] = 1;
        }
        $json = json_encode($this->token_data);
        echo $json;
    }

    /**
     * 删除接口
     */
    public function imp_delete(){
        if($this->token_data['tag']) {
            $data = $_POST['data'];
            $arr = json_decode($data, true);
            $index = count($arr);
            for($i = 0; $i<$index;){
                $this->menu_model->delete_menu($arr[$i]);
                $i++;
            }
        }
        $json = json_encode($this->token_data);
        echo $json;
    }
}